﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using DataForest.DataModel;
using DataForest.ViewModel;

namespace DataForest.View.Converter
{
    class NodeToIsSelectedConverter : Freezable, IMultiValueConverter
    {
        private System.Windows.Controls.TreeView treeView;

        public System.Windows.Controls.TreeView TreeView
        {
            get { return (System.Windows.Controls.TreeView)GetValue(TreeViewProperty); }
            set { SetValue(TreeViewProperty, value); }
        }

        public static readonly DependencyProperty TreeViewProperty =
            DependencyProperty.Register("TreeView", typeof(System.Windows.Controls.TreeView), typeof(NodeToIsSelectedConverter), new PropertyMetadata(TreeViewChanged));


        private static void TreeViewChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            NodeToIsSelectedConverter converter = d as NodeToIsSelectedConverter;
            System.Windows.Controls.TreeView tree = e.NewValue as System.Windows.Controls.TreeView;

            if (tree == null || converter == null) return;

            converter.treeView = tree;
        }


        protected override Freezable CreateInstanceCore()
        {
            return new NodeToIsSelectedConverter();
        }

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values.Length > 1) {
                Node selectedNode = values[0] as Node;
                Node currentNode = values[1] as Node;
                if (selectedNode != null && currentNode != null)
                {
                    if (selectedNode == currentNode)
                    {
                        return true;
                    }
                }

            }
            return false;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
